<?php

class Sgit_Controller_Action_Helper_Profiler extends Zend_Controller_Action_Helper_Abstract
{
    protected $_profiler;
    protected $_view;
    protected $_config;
    protected $_db;

    public function __construct($db,$cache)
    {
        $this->_db = $db;
        $this->_cache = $cache;
    }

    public function preDispatch()
    {

    	$this->t1=microtime(true);
        $this->language();
        
        
    }
    
    public function postDispatch()
    {

        $view = $this->getActionController()->view;
        $view->timeServer='Dispatch: '.round(microtime(true)-$this->t1,1).' seg';
        $this->visitas();
        
    }


    public function visitas()
    {
        $id_user=Zend_Auth::getInstance()->getIdentity()->id;
        
        if($id_user){
            $model=new Application_Model_Visitas();
    
            $request = Zend_Controller_Front::getInstance()->getRequest();
            $controller=$request->getControllerName();
            $action=$request->getActionName();
            $idZona=$request->getParam('idZona');
    
            $model->myreplace($controller,$action,$idZona,$id_user);
        }
    }



    public function language()
    {
        
        if(Zend_Auth::getInstance()->hasIdentity()){
            
            $mylocale=Zend_Auth::getInstance()->getIdentity()->locale;
            
            if(!$mylocale){
                $localeValue='es';
                $locale = new Zend_Locale('es_ES');
            }else{
                if($mylocale=='es_ES'){
                    $localeValue='es';
                    $locale = new Zend_Locale('es_ES');
                }else{
                    $localeValue='en';
                    $locale = new Zend_Locale('en_GB');
                }
            }
    
            define('LOCALE',$localeValue);
            
            Zend_Registry::set('Zend_Locale', $locale);
            
            $cache = $this->_cache;
            Zend_Translate::setCache($cache);
    
             
             //quiza haya otra forma de cachear el zend_transalte en lugar de la db 
            if( ($content=$cache->load('Translate_Array_Options_'.$localeValue)) === false ) {
                if($localeValue!='es'){
                    $dbAdapter = $this->_db;
                    $select = $dbAdapter->select()->from('translation',array('es',$localeValue));
                    $translateTable = $dbAdapter->fetchAll($select);
                    foreach($translateTable as $v){
                        $content[$v['es']]=$v[$localeValue];
                    }
                    $cache->save($content, 'Translate_Array_Options_'.$localeValue);
                }
            }
    
    
            $translate = new Zend_Translate(
                array(
                    'adapter' => 'array',
                    'content' => $content,
                    'locale'  => $localeValue
                )
            );
    
    
    
            Zend_Form::setDefaultTranslator($translate);
            Zend_Registry::set('Zend_Translate', $translate);

        }

    }

    
   
}
